Java Beans and Some Properties
JavaBeans are reusable software components with specific properties, methods (getter and setter methods for properties of class), and events that follow standardized conventions. Key concepts include bound and constrained properties, introspection, persistence, and customizers. JavaBeans are classes which implement Serializable interface with no-argument constructor
Bound and Constrained Properties
Bound and Constrained properties are two types of properties of Java Beans used to support communication between different JavaBeans components through event notification.
- Bound Properties: A bound property notifies interested objects (listeners) when its value changes. When a change occurs, the bean fires a PropertyChangeEvent to all registered PropertyChangeListener objects. Kind of Observer Pattern.
- Implementation: This is implemented using the PropertyChangeSupport helper class in the java.beans package, which simplifies listener management.
- Constrained Properties: A constrained property is a type of a bound property that allows listeners to validate and potentially accept / reject a proposed change before it is finalized.
- Implementation: When a change is attempted, the bean fires a PropertyChangeEvent to registered VetoableChangeListener objects. Any listener can throw a PropertyVetoException to stop the change from happening.
Introspection
Introspection is a mechanism using which a builder tool or application can dynamically analyze a JavaBean to discover its properties, methods, and the events it supports, without needing explicit programmatic knowledge of the bean's class.
- Mechanism: Introspection relies on Java's Reflection API and a set of standard design patterns (naming conventions) for methods (e.g., using getName() and setName() for a property named Name).
- Tools: The Introspector class in the java.beans package is a key utility that helps in obtaining a BeanInfo object, which describes the bean's features.
Persistence
Persistence is feature of a JavaBean which allows JavaBeans to save its current state (including property values and instance variables) to non-volatile storage and restore it at a later time.
- Mechanism: This is usually accomplished using Java's built-in object serialization mechanisms. A bean can be made serializable by implementing the simple java.io.Serializable marker interface.
- Storage: The state can be saved in a binary format (using ObjectOutputStream) or in an XML format.
Customizers
Customizers are a specialized user interface provided by the bean developer to help another developer configure the entire bean in a builder tool environment.
- Purpose: They are used when the configuration logic for a bean is too complex to be handled by simple property editors. A customizer can offer a complete graphical interface or a step-by-step wizard to guide the user through the customization process.
- Usage: Customizers are typically only loaded and used at design time within a development environment and are not needed while the bean is running in an application.